package mysql

import (
	"errors"
	"fmt"
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto"
	"gitee.com/captials-team/ubdframe/src/domain/dto/paginate"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/domain/models"
	mysqlClients "gitee.com/captials-team/ubdframe/src/infrastructure/clients/mysql"
	"gorm.io/gorm"
)

func NewUserThirdDao(conf configstc.DBConfig) *UserThirdDao {
	return &UserThirdDao{
		db:   mysqlClients.NewGormDB(conf),
		conf: conf,
	}
}

type UserThirdDao struct {
	db   *gorm.DB
	conf configstc.DBConfig
}

func (dao *UserThirdDao) Search(search *dto.SearchUserThirdParams, pa *paginate.Pager) ([]*models.UserThird, *paginate.Pager, error) {
	if search == nil {
		search = &dto.SearchUserThirdParams{}
	}
	if pa == nil {
		pa = &paginate.Pager{}
	}
	pa.Correct()

	db := dao.db.Model(new(models.UserThird))

	var list []*models.UserThird

	if search.ThirdType != "" {
		db = db.Where("third_type=?", search.ThirdType)
	}
	if search.Uid > 0 {
		db = db.Where("uid=?", search.Uid)
	}
	db = db.Omit("ExtendInfo")

	if ret := db.Count(&pa.Total); ret.Error != nil {
		return list, pa, ret.Error
	}

	db = db.Order("id desc")
	if ret := db.Limit(pa.Size).Offset(pa.Offset()).Find(&list); ret.Error != nil {
		return list, pa, ret.Error
	}
	pa.Correct()
	return list, pa, nil
}

func (dao *UserThirdDao) Query(id int64) (*models.UserThird, error) {
	var info models.UserThird
	ret := dao.db.Model(&info).First(&info, "id = ?", id)
	if errors.Is(ret.Error, gorm.ErrRecordNotFound) {
		return nil, nil
	}
	if ret.Error != nil {
		return nil, ret.Error
	}
	return &info, nil
}

func (dao *UserThirdDao) QueryByThirdId(thirdType string, thirdId string) (*models.UserThird, error) {
	var info models.UserThird
	ret := dao.db.Model(&info).Where("third_id = ?", thirdId).Where("third_type = ?", thirdType).First(&info)
	if errors.Is(ret.Error, gorm.ErrRecordNotFound) {
		return nil, nil
	}
	if ret.Error != nil {
		return nil, ret.Error
	}
	return &info, nil
}

func (dao *UserThirdDao) QueryByThirdUnionId(thirdType string, unionId string) (*models.UserThird, error) {
	var info models.UserThird
	ret := dao.db.Model(&info).Where("third_union_id = ?", unionId).Where("third_type = ?", thirdType).First(&info)
	if errors.Is(ret.Error, gorm.ErrRecordNotFound) {
		return nil, nil
	}
	if ret.Error != nil {
		return nil, ret.Error
	}
	return &info, nil
}

func (dao *UserThirdDao) Add(add *models.UserThird) (int64, error) {
	if add.Id > 0 {
		return 0, fmt.Errorf("not support update")
	}
	ret := dao.db.Model(add).Create(add)
	if ret.Error != nil {
		return 0, ret.Error
	}
	if ret.RowsAffected == 0 {
		return 0, fmt.Errorf("add fail")
	}

	return add.Id, nil
}

func (dao *UserThirdDao) Delete(id int64) (int64, error) {
	ret := dao.db.Where("id=?", id).Unscoped().Delete(&models.UserThird{})
	if ret.Error != nil {
		return 0, ret.Error
	}
	if ret.RowsAffected == 0 {
		return 0, fmt.Errorf("no delete")
	}
	return ret.RowsAffected, nil
}

func (dao *UserThirdDao) Migrate() (int64, error) {
	return 1, dao.db.AutoMigrate(new(models.UserThird))
}

func (dao *UserThirdDao) Use(db *gorm.DB) interfaces.ItfUserThird {
	return &UserThirdDao{db: db}
}

func (dao *UserThirdDao) DB() *gorm.DB {
	return dao.db
}
